# -*- coding: utf-8 -*-
# 按照不同的功能流程输出不同的日志文件,process_tag作为区分标志
from loguru import logger
from datetime import datetime
import os
import yaml


def time_flag():
    return f'{datetime.now():%H:%M:%S.%f}|>'


# 定义日志等级，可以后续配置修改，合理设定日志输出
with open('./config/logcfg.yml', 'r') as f:
    config_log = yaml.safe_load(f)
level_dict = config_log['level']

# 定义日志格式
step_logger_format = "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {extra[process_tag]} | {module}::{function}::{line} | {message}"


# 定义不同功能模块的日志对象
app_init_logger = logger.bind(process_tag="app_init")

chaxunfenxi_logger = logger.bind(process_tag="chaxunfenxi")
db_logger = logger.bind(process_tag="db")
# oracle_db_logger = logger.bind(process_tag="oracle_db")
# zhengbian_logger = logger.bind(process_tag="zhengbian")
# step_logger = logger.bind(process_tag="step")
# modbus_logger = logger.bind(process_tag="modbus")

# 移除默认的控制台处理器
logger.remove()

# 判断.logs文件夹是否存在，如果不存在则创建
if not os.path.exists("./logs"):
    os.makedirs("./logs")
# 配置不同日志对象的输出文件

logger.add(
    "./logs/app_init.log",
    rotation="1 MB",
    retention=5,
    format=step_logger_format,
    level=level_dict['app_init'],
    filter=lambda record: record["extra"].get(
        "process_tag") == "app_init"
)

logger.add(
    "./logs/chaxunfenxi.log",
    rotation="1 MB",
    retention=5,
    format=step_logger_format,
    level=level_dict['chaxunfenxi'],
    filter=lambda record: record["extra"].get(
        "process_tag") == "chaxunfenxi"
)

logger.add(
    "./logs/db.log",
    rotation="1 MB",
    retention=5,
    format=step_logger_format,
    level=level_dict['db'],
    filter=lambda record: record["extra"].get(
        "process_tag") == "db"
)


# logger.add(
#     "./logs/oracle_db.log",
#     rotation="1 MB",
#     retention=5,
#     format=step_logger_format,
#     level=level_dict['oracle_db'],
#     filter=lambda record: record["extra"].get(
#         "process_tag") == "oracle_db"
# )

# logger.add(
#     "./logs/zhengbian.log",
#     rotation="1 MB",
#     retention=5,
#     format=step_logger_format,
#     level=level_dict['zhengbian'],
#     filter=lambda record: record["extra"].get(
#         "process_tag") == "zhengbian"
# )

# logger.add(
#     "./logs/step.log",
#     rotation="1 MB",
#     retention=5,
#     format=step_logger_format,
#     level=level_dict['step'],
#     filter=lambda record: record["extra"].get(
#         "process_tag") == "step"
# )

# logger.add(
#     "./logs/modbus.log",
#     rotation="1 MB",
#     retention=5,
#     format=step_logger_format,
#     level=level_dict['modbus'],
#     filter=lambda record: record["extra"].get(
#         "process_tag") == "modbus"
# )

# 以下设置可以将有an的日志忽略掉
# 示例：自定义过滤函数

'''
def filter_an(record):
    return "an" not in record["message"]


logger.add(
    sink=lambda msg: None,  # 丢弃符合条件的日志
    filter=lambda record: not filter_an(record)
)

'''

# 移除默认的控制台处理器
# logger.remove()

# # 添加新的控制台处理器
# logger.add(
#     sink="stdout",
#     format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {extra[process_tag]} | {message}",
#     level="DEBUG"
# )


def test_logger():
    if 1:
        for i in range(3):
            app_init_logger.debug("这是 app_init 的 debug日志输出")
            app_init_logger.info("这是app_init的 info日志输出")
            app_init_logger.warning("这是app_init的 warn日志输出")
            app_init_logger.error("这是app_init的 error日志输出")


if __name__ == '__main__':
    test_logger()
